interface BuildTreeList {
    [x: string]: any;
}

// 转树形结构
export const buildtree = (list: BuildTreeList[], pId: string, ids: string) => {
    let pid: string = pId;
    let id: string = ids;
    // 顶级与子级数据分开
    let parents = list.filter((p: { [x: string]: number; }) => p[pid] == null || p[pid] <= 0),
        children = list.filter((c: { [x: string]: number; }) => c[pid] != null || c[pid] > 0);
    DataTree(parents, children)
    function DataTree(parents: any[], children: any[]) {
        parents.map((p: { [x: string]: any; value: string; children: any[]; }) => {
            p.value = String(p[id])
            children.map((c: { [x: string]: any; }, i: number) => {
                c[id] = String(c[id])
                if (c[pid] == p[id]) {
                    let _children = JSON.parse(JSON.stringify(children))
                    _children.splice(i, 1);
                    DataTree([c], _children)
                    if (p.children) {
                        p.children.push(c)
                    } else {
                        p.children = [c]
                    }
                }
            })
        });
    }
    return parents
}

// 转树形结构
export const buildtree1 = (list: BuildTreeList[], pId: string, ids: string) => {
    let pid: string = pId;
    let id: string = ids;
    let min = Math.min.apply(Math, list.map((item: any) => item[id]))
    // 顶级与子级数据分开
    let parents = list.filter((p: { [x: string]: number; }) => p[id] == min),
        children = list.filter((c: { [x: string]: number; }) => c[pid] > 0);
    DataTree(parents, children)
    function DataTree(parents: any[], children: any[]) {
        parents.map(p => {
            p.value = String(p[id])
            children.map((c, i) => {
                if (c[pid] == p[id]) {
                    let _children = JSON.parse(JSON.stringify(children))
                    _children.splice(i, 1);
                    DataTree([c], _children)
                    if (p.children) {
                        p.children.push(c)
                    } else {
                        p.children = [c]
                    }
                }
            })
        });
    }
    return parents
}

export const shu = (list: BuildTreeList[], pId: string, ids: string) => {
    let pid: string = pId;
    let id: string = ids;
    let _data = JSON.parse(JSON.stringify(list))
    let min = Math.min.apply(Math, _data.map((item: { [x: string]: any; }) => item.cataloglen))
    let min_arr = _data.filter((c: any) => c.cataloglen == min)
    min_arr.forEach((e: { [x: string]: any }) => {
        _data.forEach((item: any, i: number) => {
            if (item.cataloglen == e.cataloglen) {
                _data.splice(i, 1)
            }
        })
    })
    DataTree(min_arr, _data)
    function DataTree(parents: any[], children: any[]) {
        parents.map(p => {
            p.value = String(p[id])
            children.map((c, i) => {
                if (c[pid] == p[id]) {
                    let _children = JSON.parse(JSON.stringify(children))
                    _children.splice(i, 1);
                    DataTree([c], _children)
                    if (p.children) {
                        p.children.push(c)
                    } else {
                        p.children = [c]
                    }
                }
            })
        });
    }
    return min_arr
}

interface List {
    [x: string]: any;
    children: any[];
}

export const isLeaf = (list: List[]) => {
    list.forEach((e: { [x: string]: any; }) => {
        if (e.children) {
            isLeaf(e.children)
        } else {
            e.isLeaf = true
        }
    });
}
